import grails.converters.JSON
import de.keychain.Configuration
import de.keychain.Password
import de.keychain.ServiceModel

class BootStrap {

    def init = { servletContext ->
		
		developmentSetup()

		addJsonMarshaller()
    }
	
    def destroy = {
    }
	
	def developmentSetup(){
		def systemPassword = new Password(password:"admin")
		systemPassword.encodePassword()
		
		systemPassword.save()
		
		def systemService = new ServiceModel(name:"KeyChain", username:"admin")
		
		systemService.save()
		
		systemService.addToPasswords(systemPassword)
		
		def configuration = new Configuration(email:"unit-test@mail.de")
		configuration.service = systemService
		
		configuration.save()
	}
	
	def addJsonMarshaller(){
		JSON.registerObjectMarshaller(ServiceModel) {
			def output = [:]
			output['id'] = it.id
			output['username'] = it.username
			output['name'] = it.name
			output['password'] = it.getCurrentPassword()
		
			return output;
		}
	}
}
